package com.pgeneral.user.management.controller;

import com.pgeneral.user.management.config.ResultBean;
import com.pgeneral.user.management.dto.GlobalInfosDTO;
import com.pgeneral.user.management.entity.UserInfo;
import com.pgeneral.user.management.service.impl.UserInfoServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 用户的增删改查接口
 */
@Slf4j
@RestController
@RequestMapping("/userinfo")
public class UserInfoController {
    @Autowired
    private UserInfoServiceImpl userinfoService;

    /**
     * 添加用户信息
     *
     * @param userInfos
     * @return
     */
    @RequestMapping(value = "/saveInfo", method = RequestMethod.POST)
    public ResultBean insertUserInfoTable(@RequestBody List<UserInfo> userInfos) {
        log.info("调用用户save方法，方法参数为：{}", userInfos);
        int result = userinfoService.insert(userInfos);
        return ResultBean.InsertSuccessResult(result);

    }

    /**
     * 用户查询(不传参就全表返回)
     *
     * @param userInfo
     * @return
     */
    @RequestMapping(value = "/getInfo", method = RequestMethod.POST)
    public ResultBean getUserInfoTable(@RequestBody UserInfo userInfo) {
        log.info("调用用户get方法，方法参数为：{}", userInfo);
        List<UserInfo> userInfoTable = userinfoService.getUserInfoTable(userInfo);
        return ResultBean.Success(userInfoTable);

    }

    /**
     * 用户查询(不传参就全表返回)
     */
    @PostMapping(value = "/getAllUserInfos")
    public ResultBean getAllUserInfos(){
        log.info("调用用户全表get方法");
        List<UserInfo> allUserInfos = userinfoService.getAllUserInfos();
        return ResultBean.Success(allUserInfos);
    }

    /**
     * 用户更新
     *
     * @param userInfos
     * @return
     */
    @RequestMapping(value = "/updateInfo", method = RequestMethod.POST)
    public ResultBean updateUserInfoTable(@RequestBody UserInfo userInfos) {
        log.info("：{}", userInfos);
        int result = userinfoService.updateUserInfoTable(userInfos);
        return ResultBean.InsertSuccessResult(result);

    }

    /**
     * 用户删除
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/deleteInfo", method = RequestMethod.POST)
    public ResultBean deleteUserInfoTable(@RequestBody List<String> ids) {
        System.out.println("用户delete");
        log.info("调用用户delete方法，方法参数为：{}", ids);
        int result = userinfoService.updateIsDeletefromUserInfoTable(ids);
        return ResultBean.InsertSuccessResult(result);
    }

    /**
     * 五表联查
     *
     * @return
     */
    @RequestMapping(value = "/getInfos", method = RequestMethod.POST)
    public ResultBean getGlobalInfos(@RequestBody UserInfo userInfo) {
        log.info("调用五表联查方法,用户表，角色表，科室表，权限表，权限角色关系表");
        List<GlobalInfosDTO> globalInfosDTOS = userinfoService.getGlobalInfos(userInfo);
        return ResultBean.Success(globalInfosDTOS);
    }

    /**
     * 根据用户UserID修改用户登录密码
     */
    @RequestMapping(value = "/updatePasswordByUserId", method = RequestMethod.POST)
    public ResultBean updatePassWordFromUserTableByUserId(@RequestBody UserInfo userInfo) {
        log.info("调用根据userId修改密码的方法");
        int i = userinfoService.updatePasswordByUserId(userInfo);
        return ResultBean.InsertSuccessResult(i);
    }

    /**
     * 三表联查
     */
    @RequestMapping(value = "/getThreeInfos", method = RequestMethod.POST)
    public ResultBean getThreeTable(@RequestBody UserInfo userInfo) {
        log.info("调用三表联查方法用户表，角色表，科室表，");

        List<GlobalInfosDTO> getThreeTable = userinfoService.getThreeTable(userInfo);
        return ResultBean.Success(getThreeTable);
    }

    /**
     * 五表联查为登录写
     *
     * @return
     */
    @RequestMapping(value = "/getLoginUserInfo", method = RequestMethod.POST)
    public ResultBean getLoginUser(@RequestBody UserInfo userInfo) {
        log.info("调用登录接口五表联查方法,用户表，角色表，科室表，权限表，权限角色关系表,{}", userInfo);
        List<GlobalInfosDTO> globalInfosDTOS = userinfoService.getLoginUser(userInfo);
        return ResultBean.Success(globalInfosDTOS);
    }

    /**
     * 五表联查为paid登录写
     *
     * @return
     */
    @RequestMapping(value = "/getLoginUserInfoForPaid", method = RequestMethod.POST)
    public ResultBean getLoginUserForPaid(@RequestBody UserInfo userInfo) {
        log.info("调用登录接口五表联查方法,用户表，角色表，科室表，权限表，权限角色关系表,{}", userInfo);
        List<GlobalInfosDTO> globalInfosDTOS = userinfoService.getLoginUser(userInfo);
        return ResultBean.Success(globalInfosDTOS);
    }

    /**
     * 查询用户签名地址
     *
     * @param userId
     * @return
     */
    @RequestMapping(value = "/getSign", method = RequestMethod.GET)
    public ResultBean getSign(@RequestParam List<String> userId) {
        log.info("调用用户get方法，方法参数为：{}", userId);
        List<UserInfo> lists = userinfoService.getSign(userId);
        return ResultBean.Success(lists);

    }
}
